﻿using System;

using System.Net;
using NWMES.Mes;
using NWMES.MiGetCustomerBarcodeDataServiceService;
using Newtonsoft.Json;
using NWMES;


namespace MyMes.Mes
{

    /// <summary>
    /// 
    /// 物料编码校验接口
    /// 
    /// http://ndmes7.catlbattery.com:8103/atlmeswebservice/MiGetCustomerBarcodeDataServiceService?wsdl
    /// </summary>
    public class MESGetCustomerBarcodeData : MESBaseService
    {
        static string DefaultApiSection = "GetCustomerBarcodeData";
        public static NetworkCredential networkCredential = null;
        public static MESGetCustomerBarcodeData Params = null;

        static MESGetCustomerBarcodeData()
        {
            Params = config[DefaultApiSection].ToObject<MESGetCustomerBarcodeData>();
            networkCredential = new NetworkCredential(Params.UserName, Params.Password);
        }

        /// <summary>
        /// 物料编码校验接口
        /// </summary>
        /// <param name="sfc"></param>
        /// <returns></returns>
        public static MesResult RequestMes(string sfc, string ApiSection)
        {
            MiGetCustomerBarcodeDataServiceService getCustomerBarcodeDataServiceService;
            getCustomerBarcodeDataResponse Response;
            getCustomerBarcodeData getCustomerBarcodedata;
            if (!string.IsNullOrEmpty(ApiSection))
            {
                Params = config[ApiSection].ToObject<MESGetCustomerBarcodeData>();
                networkCredential = new NetworkCredential(Params.UserName, Params.Password);
            }
            else
            {
                ApiSection = DefaultApiSection;
            }
            string ErrorMessage = Params.ParameterVerification();
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                return new MesResult(-99, ErrorMessage);
            }
            SetRequestParams(sfc, out getCustomerBarcodeDataServiceService, out Response, out getCustomerBarcodedata);

            MesResult result = MesResult.InitMesResult(JsonConvert.SerializeObject(getCustomerBarcodedata));
            try
            {
                Response = getCustomerBarcodeDataServiceService.getCustomerBarcodeData(getCustomerBarcodedata);

                result.EndTime = DateTime.Now;
                var ret = Response.@return;
                result.Message = ret?.message;
                result.Code = ret.code;
                result.Success = result.Code == 0;
                result.ResponeJson = JsonConvert.SerializeObject(Response);

                result.Respone = Response;
                result.ret = ret;
                result.customerBarcode = ret?.customerBarcode;
            }
            catch (Exception ex)
            {
                result.Code = -99;
                result.Message = ex.Message;
            }

            SaveMesLog(ApiSection, result);
            return result;

        }

        private static void SetRequestParams(string sfc, out MiGetCustomerBarcodeDataServiceService getCustomerBarcodeDataServiceService, out getCustomerBarcodeDataResponse Response, out getCustomerBarcodeData getCustomerBarcodedata)
        {
            getCustomerBarcodeDataServiceService = new MiGetCustomerBarcodeDataServiceService();
            getCustomerBarcodeDataServiceService.Url = Params.Url;
            getCustomerBarcodeDataServiceService.Timeout = Params.Timeout;
            getCustomerBarcodeDataServiceService.Credentials = networkCredential;
            getCustomerBarcodeDataServiceService.PreAuthenticate = true;
            getCustomerBarcodeDataRequest getCustomerBarcodeDataRequest = new getCustomerBarcodeDataRequest();

            //站点
            getCustomerBarcodeDataRequest.site = Params.Site;
            //传入sfc
            getCustomerBarcodeDataRequest.sfc = sfc;

            Response = new getCustomerBarcodeDataResponse();
            getCustomerBarcodedata = new getCustomerBarcodeData();
            getCustomerBarcodedata.Request = getCustomerBarcodeDataRequest;
        }
        public string customerBarcode { get; private set; } = "";
    }
}
